//
//  MovieDetalViewController.m
//  UI-豆瓣项目APP
//
//  Created by dllo on 15/10/28.
//  Copyright © 2015年 孙鹏. All rights reserved.
//

#import "MovieDetalViewController.h"
#import "AppTools.h"
#import "Movielist.h"
#import "MBProgressHUD.h"
#import "UIImageView+WebCache.h"
#import "DateBaseSingleton.h"
#import "MovieDetalViewController.h"
#import "UserLandViewController.h"
@interface MovieDetalViewController ()

@property (nonatomic,retain)MBProgressHUD *hud;

@end

@implementation MovieDetalViewController

- (void)dealloc
{
    [_movieDV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIScrollView *movieDetailSC = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    
    movieDetailSC.backgroundColor = [UIColor whiteColor];
  
    
    self.movieDV = [[MovieDetailView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    
    self.movieDV.backgroundColor = [UIColor whiteColor];
    self.movieDV.amovie = self.amovie;

    [movieDetailSC addSubview:self.movieDV];
    
    
    [self.view addSubview:movieDetailSC];
    
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hud show:YES];
    self.hud.labelText = @"正在加载中...";
    
//    
//    Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
//    switch ([r currentReachabilityStatus]) {
//        case NotReachable:
//            NSLog(@"没有网络连接");
//            Movielist *movie = [[DateBaseSingleton shareDateBase]selectMovieDetail];
//
//            self.amovie = movie;
//
//            // 没有网络连接
//            break;
//        case ReachableViaWWAN:
//            // 使用3G网络
//            NSLog(@"使用3G网络");
//            
//            break;
//        case ReachableViaWiFi:
//            // 使用WiFi网络
//            NSLog(@"使用WiFi网络");
//            
//            break;
//    }
//    
    
    NSString *url = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=%@",self.amovie.movieId];
    NSLog(@"----------%@",self.amovie.movieId);
    self.navigationItem.title = self.amovie.title;
    [AppTools getDataWithFileURL:url block:^(NSMutableDictionary *result) {
        
        [[DateBaseSingleton shareDateBase]dropMovieDetailTable];
        [[DateBaseSingleton shareDateBase]creatMovieDetailTable];
        
        NSMutableDictionary *dic = [result objectForKey:@"result"];
        
//        self.movieDV.amovie = [[Movielist alloc]init];
        [self.movieDV.amovie setValuesForKeysWithDictionary:dic];
        
        [[DateBaseSingleton shareDateBase]insertMovieDetail:self.movieDV.amovie];
        
        //传值有问题,得点击两次
        self.navigationItem.title = self.amovie.title;
        self.movieDV.grade.text = self.amovie.rating;
        self.movieDV.runtime.text = self.amovie.runtime;
        self.movieDV.data.text = self.amovie.release_date;
        self.movieDV.country.text = self.amovie.country;
        self.movieDV.genres.text = self.amovie.genres;
        self.movieDV.actors.text = self.amovie.actors;
        self.movieDV.plot.text = self.amovie.plot_simple;
        NSString *rating = [NSString stringWithFormat:@"(%@评论)",self.amovie.rating_count];
        self.movieDV.rating_count.text = rating;
        [self.movieDV.plot sizeToFit];
        [self.movieDV.actors sizeToFit];
        
        movieDetailSC.contentSize = CGSizeMake(375, self.movieDV.plot.frame.size.height + 400);
       
        
        NSURL *url = [NSURL URLWithString:self.amovie.pic_url];
        [self.movieDV.headImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHoderImage"]];
        
        [self.hud hide:YES];
        
        
    }];
    
    [self.hud hide:YES];
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_share"] style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];
    

    
    
    
    // Do any additional setup after loading the view.
}

- (void)buttonAction:(UIBarButtonItem *)btn{

    NSString *login = [[NSUserDefaults standardUserDefaults]objectForKey:@"isLogin"];

    
    if ([login isEqualToString:@"1"]) {
        
        
        
        [[DateBaseSingleton shareDateBase]creatMovieUserTable];
        
        self.amovie.userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
        
        
        
        
        NSMutableArray *array = [NSMutableArray arrayWithArray:
        array = [[DateBaseSingleton shareDateBase]selectMovieUserName:self.amovie.userName]];
        
        NSMutableArray *nameArray = [NSMutableArray array];
        
        for (Movielist *movie in array) {
            
            [nameArray addObject:movie.movieName];
            
            
        }
        
        
        NSLog(@"-=-==%@",array);
        
        
        
        if ([nameArray containsObject:self.amovie.movieName]) {
            
            
            
            
            
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"该电影已经被收藏" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alertC animated:YES completion:^{
                
                
            }];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                
            }];
            [alertC addAction:action];
            
            
        }
        
        else
        {
            
            self.amovie.userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
            [[DateBaseSingleton shareDateBase]insertMovieUser:self.amovie];//插入
            
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alertC animated:YES completion:^{
                
                
            }];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                
            }];
            [alertC addAction:action];
            
            
            
            
        }
        
        
    }else
    {
        
        
        UserLandViewController *userLandVC = [[UserLandViewController alloc]init];
        
        [self.navigationController pushViewController:userLandVC animated:YES];
        
        
    }
    

    
    

}

//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//    
//    
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        
//        
//        NSString *userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
//        
//        
//        
//        NSMutableArray *array = [NSMutableArray array];
//        
//        array = [[DateBaseSingleton shareDateBase]selectMovieUserName:userName];
//        NSString *titleName = [array objectAtIndex:indexPath.row];
//        
//        [[DateBaseSingleton shareDateBase]deleteActivityDetail:titleName];
//        
//        
//        [self.userActivityDetailVC reloadData];
//        
//    }
//    
//}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
